package com.sample.FinalProject;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
import android.widget.TextView;

public class FinalView extends View implements OnTouchListener{
	long LastMove;
	Random ran=new Random();
	final static int delay = 1000;
	private int isBuying=0;
	private static int mode=3;
	private static int AiMode=2;
	private boolean updateText=true;
	private boolean gameEnded=false;
	private static int gold=1000;
	private static int AiGold=0;
	private static int AiTroops=3;
	private static int interval=0;
	int[] target=new int[2];
	private int temp2, temp3;
	private Paint paint1;
	TextView text;
	private int moveX, moveY;
	private static int troops=0;
	MainShape main=new MainShape();
	Rect MainRect=new Rect();
	ArrayList <Building> buildings= new ArrayList<Building>();
	ArrayList <Turret> towers=new ArrayList<Turret>();
	ArrayList <Square> squares=new ArrayList<Square>();
	ArrayList <Circle> CircleUnits=new ArrayList<Circle>();
	
	public FinalView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setFocusable(true);
		setFocusableInTouchMode(true);
		paint1=new Paint();
		this.setOnTouchListener(this);
		}
	
	public void onDraw(Canvas c) {
	long now = System.currentTimeMillis();
	if (gameEnded==false && now - LastMove > delay){	
	if (isBuying==0){	
	update();
	}
	if (updateText==true){
	text.setText("Gold: " + gold + " Health: " + main.getHealth() + "/" + main.getMaxHealth() + " XP: " + main.getXP() + "/" + main.getMaxXP());
	}
	for (int w = 0; w<buildings.size(); w++){
		paint1.setColor(buildings.get(w).getColor());
		c.drawRect(new Rect(buildings.get(w).getX(), buildings.get(w).getY(), buildings.get(w).getX()+buildings.get(w).getLength(), buildings.get(w).getY()+buildings.get(w).getLength()), paint1);
		}
	for (int z = 0; z<towers.size(); z++){
		paint1.setColor(towers.get(z).getColor());
		c.drawCircle(towers.get(z).getX(), towers.get(z).getY(), 25, paint1);
		if (towers.get(z).isShooting()){
		paint1.setColor(Color.GRAY);	
		c.drawLine(towers.get(z).getX(), towers.get(z).getY(), target[0], target[1], paint1);	
		}
	}
	for (int x = 0; x<squares.size(); x++){
		paint1.setColor(squares.get(x).getColor());
		c.drawRect(new Rect(squares.get(x).getX(), squares.get(x).getY(), squares.get(x).getX()+squares.get(x).getLength(), squares.get(x).getY()+squares.get(x).getLength()), paint1);
		}
	for (int y = 0; y<CircleUnits.size(); y++){
		paint1.setColor(CircleUnits.get(y).getColor());
		c.drawCircle(CircleUnits.get(y).getX(), CircleUnits.get(y).getY(), 12 , paint1);
		}
	
	paint1.setColor(main.getColor());
	MainRect.set(main.getX(), main.getY(), main.getX()+main.getLength(), main.getY()+main.getLength());	
	c.drawRect(MainRect, paint1);
	LastMove = now;
FinalHandler.sleep(delay);
	}}
	public boolean onTouch(View v, MotionEvent event) {
	moveX=(int)event.getRawX();
	moveY=(int)event.getRawY();
	if (isBuying!=0){
	if (isBuying==1 && collisionCheck(new Circle(moveX, moveY, Color.WHITE, false), -1, 0) && moveX>300 && moveY>450){
	towers.add(new Turret(moveX, moveY-120, Color.WHITE));	
	isBuying=0;
	gold-=1000-10*main.getINT();
	text.setText("Please Go Back to the Shop to Continue");
	}
	else if (isBuying==1){
	text.setText("Cannot Place Tower There");	
	}
	else if (isBuying==2 && collisionCheck(new Square(moveX, moveY, Color.WHITE, false), -1, 0) && moveX>300 && moveY>450){
	buildings.add(new Building(moveX, moveY-120, Color.WHITE, 2));
	isBuying=0;	
	gold-=2000-20*main.getINT();
	text.setText("Please Go Back to the Shop to Continue");
	}	
	else if (isBuying==2){
	text.setText("Cannot Place Fountain There");	
	}
	}	
	for (int i=0; i<towers.size(); i++){
	if (towers.get(i).getY()-towers.get(i).getRadius()<= moveY-120 && towers.get(i).getY() + towers.get(i).getRadius()>= moveY-120 && towers.get(i).getX() + towers.get(i).getRadius()>= moveX && towers.get(i).getX() - towers.get(i).getRadius()<= moveX){
	text.setText("Tower: " + towers.get(i).getHealth() + "/" + towers.get(i).getMaxHealth());	
	updateText=false;
	}	
	else{
	updateText=true;	
	}
	}
	for (int i=0; i<buildings.size(); i++){
	Rect tempRect=new Rect(buildings.get(i).getX(), buildings.get(i).getY(), buildings.get(i).getX()+buildings.get(i).getLength(), buildings.get(i).getY()+buildings.get(i).getLength());	
	if(tempRect.contains(moveX, moveY-120)){
	text.setText("Structure: " + buildings.get(i).getHealth() + "/" + buildings.get(i).getMaxHealth());	
	updateText=false;	
	}	
	else{
		updateText=true;	
	}
	}
	for (int i=0; i<CircleUnits.size(); i++){
		if (CircleUnits.get(i).getY()-CircleUnits.get(i).getRadius()<= moveY-120 && CircleUnits.get(i).getY() + CircleUnits.get(i).getRadius()>= moveY-120 && CircleUnits.get(i).getX() + CircleUnits.get(i).getRadius()>= moveX && CircleUnits.get(i).getX() - CircleUnits.get(i).getRadius()<= moveX){
			text.setText("Unit: " + CircleUnits.get(i).getHealth() + "/" + CircleUnits.get(i).getMaxHealth());	
			updateText=false;
		}
		else{
			updateText=true;	
			}
		}
	for (int i=0; i<squares.size(); i++){
	Rect tempRect=new Rect(squares.get(i).getX(), squares.get(i).getY(), squares.get(i).getX()+squares.get(i).getLength(), squares.get(i).getY()+squares.get(i).getLength());	
	if(tempRect.contains(moveX, moveY-120)){
	text.setText("Units: " + squares.get(i).getHealth() + "/" + squares.get(i).getMaxHealth());	
	updateText=false;
	}	
	else{
		updateText=true;	
		}
	}
	return true;
	}
	private void update(){
	//long now = System.currentTimeMillis();
	//if (now - LastMove > delay){
	aiThink();
	gold++;
	AiGold+=2+interval/2;
	main.maintain();
	for (int w = 0; w<buildings.size(); w++){
		buildings.get(w).maintain();
		if (w>1){
		buildings.get(w).heal(this);	
		}
		}
	for (int x = 0; x<squares.size(); x++){
		squares.get(x).maintain();
		if (squares.get(x).isEnemy()==false){
		if (collisionCheck(squares.get(x).think(squares.get(x), mode, this), x, 1)==true){	
		squares.get(x).mockUpdate();	
		}
		}
		else if (squares.get(x).isEnemy()==true){
		if (collisionCheck(squares.get(x).think(squares.get(x), AiMode, this), x, 1)==true){	
		squares.get(x).mockUpdate();	
		}	
		}
		//else if (collisionCheck(squares.get(x), x, 0)== true){
		//squares.get(x).reverse();	
		//}
		}
	for (int y = 0; y<CircleUnits.size(); y++){
		CircleUnits.get(y).maintain();
		if (CircleUnits.get(y).isEnemy()==false){
		if (collisionCheck(CircleUnits.get(y).think(CircleUnits.get(y), mode, this), y, 1)==true){
		CircleUnits.get(y).mockUpdate();
		}
		}
		else if (CircleUnits.get(y).isEnemy()==true){
			if (collisionCheck(CircleUnits.get(y).think(CircleUnits.get(y), AiMode, this), y, 1)==true){
			CircleUnits.get(y).mockUpdate();
			}	
		}
		//else if (collisionCheck(CircleUnits.get(y), y, 0) == true){
		//CircleUnits.get(y).reverse();	
		//}
}
	for (int z = 0; z<towers.size(); z++){
		towers.get(z).maintain();
		towers.get(z).notShooting();
		target= towers.get(z).target(this);
	}
	
	if(moveX>0 && moveY>0){	
	if(mainCollisionCheck()){
	main.move(moveX, moveY);
	}
	else{
	main.move(moveX, moveY, -1*main.getSpeed());	
	moveX=-1;
	moveY=-1;
	}
	}
	if(moveX-main.getX()<6 && moveX-main.getX()>-6 && moveY-main.getY()<126 && moveY-main.getY()>114){
	moveX=0;
	moveY=0;
	}
	if (buildings.get(1).getHealth()<=0){
		text.setText("You Win");	
		gameEnded=true;
		}	
		else if(buildings.get(0).getHealth()<=0 || main.getHealth()<=0){
		text.setText("You Lose");	
		gameEnded=true;
		}
	deathCheck();
	//LastMove = now;
//FinalHandler.sleep(delay);
	}
		
	//}
	
	
	
	
	
	
	
	
	private void aiThink(){
	if (AiTroops>=5+interval && AiMode != 1){
	interval+=2;
	AiMode=1;
	}	
	else if (AiTroops<interval){
	AiMode=2;	
	}
	else if (AiMode!= 1){
	AiMode=3;	
	}
	if (interval<10){
	if (AiGold>=100){
	do{
		temp2=ran.nextInt(200);
		temp3=ran.nextInt(300);
		}
		while(collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false);
		squares.add(new Square(temp2, temp3, Color.RED, false));
		AiTroops++;
		AiGold-=100;
	}	
	}
	if (interval>= 10 && interval<20){
	if (AiTroops<5){
		if (AiGold>=100){
			do{
				temp2=ran.nextInt(150);
				temp3=ran.nextInt(200);
				}
				while(collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false);
				squares.add(new Square(temp2, temp3, Color.RED, false));
				AiTroops++;
				AiGold-=100;
			}		
	}
	else{
		if (AiGold>=150){
			do{temp2=ran.nextInt(150);
				temp3=ran.nextInt(200);
				
				}
				while(collisionCheck(new Circle(temp2, temp3, Color.GREEN, false), -1, 0)==false);
				CircleUnits.add(new Circle(temp2, temp3, Color.RED, false));
				AiTroops++;
				AiGold-=150;
			}		
	}
	}
	if(interval>=20 && interval<30){
		if (AiTroops<5){
			if (AiGold>=100){
				do{
					temp2=ran.nextInt(150);
					temp3=ran.nextInt(200);
					}
					while(collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false);
					squares.add(new Square(temp2, temp3, Color.RED, false));
					AiTroops++;
					AiGold-=100;
				}		
		}
		else if (AiTroops<10){
			if (AiGold>=150){
				do{temp2=ran.nextInt(150);
					temp3=ran.nextInt(200);
					
					}
					while(collisionCheck(new Circle(temp2, temp3, Color.GREEN, false), -1, 0)==false);
					CircleUnits.add(new Circle(temp2, temp3, Color.RED, false));
					AiTroops++;
					AiGold-=150;
				}		
		}
		else{
				if (AiGold>=500){
					do{
						temp2=ran.nextInt(150);
						temp3=ran.nextInt(200);
						}
						while(collisionCheck(new Square(temp2, temp3, Color.GREEN, true), -1, 0)==false);
						squares.add(new Square(temp2, temp3, Color.YELLOW, true));
						AiGold-=500;
						
		}
	}}
	if (interval>=30){
		if (AiTroops<5){
			if (AiGold>=100){
				do{
					temp2=ran.nextInt(150);
					temp3=ran.nextInt(200);
					}
					while(collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false);
					squares.add(new Square(temp2, temp3, Color.RED, false));
					AiTroops++;
					AiGold-=100;
				}		
		}
		else if (AiTroops<10){
			if (AiGold>=150){
				do{temp2=ran.nextInt(150);
					temp3=ran.nextInt(200);
					
					}
					while(collisionCheck(new Circle(temp2, temp3, Color.GREEN, false), -1, 0)==false);
					CircleUnits.add(new Circle(temp2, temp3, Color.RED, false));
					AiTroops++;
					AiGold-=150;
				}		
		}
		else if (AiTroops<15){
				if (AiGold>=500){
					do{
						temp2=ran.nextInt(150);
						temp3=ran.nextInt(200);
						}
						while(collisionCheck(new Square(temp2, temp3, Color.GREEN, true), -1, 0)==false);
						squares.add(new Square(temp2, temp3, Color.YELLOW, true));
						AiGold-=500;
						
		}
	}
		else{
			if (AiGold>=750){
				do{
					temp2=ran.nextInt(150);
					temp3=ran.nextInt(200);
					}
					while(collisionCheck(new Circle(temp2, temp3, Color.GREEN, true), -1, 0)==false);
					CircleUnits.add(new Circle(temp2, temp3, Color.YELLOW, true));
					AiGold-=750;
					
	}	
		}	
	}
	}
	
	private void deathCheck(){
	for (int w = 0; w<buildings.size(); w++){
	if (buildings.get(w).getHealth()<=0 && buildings.get(w).getType() != 1){
	if (buildings.get(w).isEnemy()){
	gold+=300;
	main.setXP(main.getXP()+30);
	}
	else{
	AiGold+=300;	
	}
	buildings.remove(w);
	}	
	}
	for (int x = 0; x<squares.size(); x++){
		if (squares.get(x).getHealth()<=0){
		if(squares.get(x).isElite()){
		if (squares.get(x).isEnemy()){
			gold+=50;
			main.setXP(main.getXP()+9);
			AiTroops--;
			}
			else{
			AiGold+=50;	
			troops--;
			}	
		}	
		else{
			if (squares.get(x).isEnemy()){
				gold+=20;
				main.setXP(main.getXP()+3);
				AiTroops--;
				}
				else{
				AiGold+=20;	
				troops--;
				}	
		}
			
			squares.remove(x);
			}	
	}
	for (int y = 0; y<CircleUnits.size(); y++){
		if (CircleUnits.get(y).getHealth()<=0){
			if(CircleUnits.get(y).isElite()){
			if (CircleUnits.get(y).isEnemy()){
				gold+=75;
				main.setXP(main.getXP()+10);
				AiTroops--;
				}
				else{
				AiGold+=75;	
				troops--;
				}	
			}	
			else{
				if (CircleUnits.get(y).isEnemy()){
					gold+=30;
					main.setXP(main.getXP()+5);
					AiTroops--;
					}
					else{
					AiGold+=30;	
					troops--;
					}	
			}
				
				CircleUnits.remove(y);
				}	
	}
	for (int z = 0; z<towers.size(); z++){
		if (towers.get(z).getHealth()<=0){
			if (towers.get(z).isEnemy()){
			gold+=200;
			main.setXP(main.getXP()+20);
			}
			else{
			AiGold+=200;	
			}
			towers.remove(z);
			}		
	}
	if (main.getXP()>main.getMaxXP()){
	main.levelUp();	
	}
	}
	public boolean mainCollisionCheck(){
	boolean hasNotCollided=true;	
	for (int w = 0; w<buildings.size(); w++){
		if(main.getX()>buildings.get(w).getX() && main.getX()<buildings.get(w).getX()+buildings.get(w).getLength() || main.getX()+main.getLength() > buildings.get(w).getX() && main.getX()+main.getLength() < buildings.get(w).getX()+buildings.get(w).getLength()){
		if(main.getY()>buildings.get(w).getY() && main.getY()<buildings.get(w).getY()+buildings.get(w).getLength() || main.getY()+main.getLength() > buildings.get(w).getY() && main.getY()+main.getLength() < buildings.get(w).getY()+buildings.get(w).getLength()){
		hasNotCollided=false;
		if (buildings.get(w).isEnemy){
		buildings.get(w).setHealth(buildings.get(w).getHealth()-main.getDamage());	
		}
		} 	
		}	
			}
	for (int x = 0; x<squares.size(); x++){
		if(main.getX()>squares.get(x).getX() && main.getX()<squares.get(x).getX()+squares.get(x).getLength() || main.getX()+main.getLength() > squares.get(x).getX() && main.getX()+main.getLength() < squares.get(x).getX()+squares.get(x).getLength()){
			if(main.getY()>squares.get(x).getY() && main.getY()<squares.get(x).getY()+squares.get(x).getLength() || main.getY()+main.getLength() > squares.get(x).getY() && main.getY()+main.getLength() < squares.get(x).getY()+squares.get(x).getLength()){
			if (squares.get(x).isEnemy){
			hasNotCollided=false;		
			squares.get(x).setHealth(squares.get(x).getHealth()-main.getDamage());	
			main.setHealth(main.getHealth()-squares.get(x).getDamage());
			}
			else{
			squares.get(x).move(moveX, moveY, main.getSpeed());	
			}
			} 	
			}			
	}
	for (int y = 0; y<CircleUnits.size(); y++){
		if(main.getX()>CircleUnits.get(y).getX()-CircleUnits.get(y).getRadius() && main.getX()<CircleUnits.get(y).getX()+CircleUnits.get(y).getRadius() || main.getX()+main.getLength() > CircleUnits.get(y).getX()-CircleUnits.get(y).getRadius() && main.getX()+main.getLength() < CircleUnits.get(y).getX()+CircleUnits.get(y).getRadius()){
			if(main.getY()>CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && main.getY()<CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius() || main.getY()+main.getLength() > CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && main.getY()+main.getLength() < CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius()){	
			if (CircleUnits.get(y).isEnemy){
			hasNotCollided=false;	
			CircleUnits.get(y).setHealth(CircleUnits.get(y).getHealth()-main.getDamage());	
			main.setHealth(main.getHealth()-CircleUnits.get(y).getDamage());
			}
			else{
			CircleUnits.get(y).move(moveX, moveY, main.getSpeed());	
			}
			} 	
			}		
	}
	for (int z = 0; z<towers.size(); z++){
		if(main.getX()>towers.get(z).getX() && main.getX()<towers.get(z).getX()+towers.get(z).getLength() || main.getX()+main.getLength() > towers.get(z).getX() && main.getX()+main.getLength() < towers.get(z).getX()+towers.get(z).getLength()){
			if(main.getY()>towers.get(z).getY()-towers.get(z).getRadius() && main.getY()<towers.get(z).getY()+towers.get(z).getRadius() || main.getY()+main.getLength() > towers.get(z).getY()-towers.get(z).getRadius() && main.getY()+main.getLength() < towers.get(z).getY()+towers.get(z).getRadius()){
			hasNotCollided=false;	
			if (towers.get(z).isEnemy){
			towers.get(z).setHealth(towers.get(z).getHealth()-main.getDamage());	
			}
			} 	
			}		
	}
	return hasNotCollided;	
	}
	public boolean collisionCheck(Square r, int i, int damaging){
		boolean hasNotCollided=true;
		for (int w = 0; w<buildings.size(); w++){
		if(r.getMockX()>buildings.get(w).getX() && r.getMockX()<buildings.get(w).getX()+buildings.get(w).getLength() || r.getMockX()+r.getLength() > buildings.get(w).getX() && r.getMockX()+r.getLength() < buildings.get(w).getX()+buildings.get(w).getLength()){
		if(r.getMockY()>buildings.get(w).getY() && r.getMockY()<buildings.get(w).getY()+buildings.get(w).getLength() || r.getMockY()+r.getLength() > buildings.get(w).getY() && r.getMockY()+r.getLength() < buildings.get(w).getY()+buildings.get(w).getLength()){
		hasNotCollided=false;
		if (buildings.get(w).isEnemy!= r.isEnemy() && damaging==1){
		buildings.get(w).setHealth(buildings.get(w).getHealth()-r.getDamage());	
		}
		} 	
		}	
			}
		for (int x = 0; x<squares.size(); x++){
			if(i!= x && r.getMockX()>squares.get(x).getX() && r.getMockX()<squares.get(x).getX()+squares.get(x).getLength() || i!= x && r.getMockX()+r.getLength() > squares.get(x).getX() && r.getMockX()+r.getLength() < squares.get(x).getX()+squares.get(x).getLength()){
				if(r.getMockY()>squares.get(x).getY() && r.getMockY()<squares.get(x).getY()+squares.get(x).getLength() || r.getMockY()+r.getLength() > squares.get(x).getY() && r.getMockY()+r.getLength() < squares.get(x).getY()+squares.get(x).getLength()){
				hasNotCollided=false;	
				if (squares.get(x).isEnemy!= r.isEnemy() && damaging==1){
				squares.get(x).setHealth(squares.get(x).getHealth()-r.getDamage());	
				r.setHealth(r.getHealth()-squares.get(x).getDamage());
				}
				} 	
				}		
			}
		for (int y = 0; y<CircleUnits.size(); y++){
			if(r.getMockX()>CircleUnits.get(y).getX() && r.getMockX()<CircleUnits.get(y).getX()+CircleUnits.get(y).getLength() || r.getMockX()+r.getLength() > CircleUnits.get(y).getX() && r.getMockX()+r.getLength() < CircleUnits.get(y).getX()+CircleUnits.get(y).getLength()){
				if(r.getMockY()>CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && r.getMockY()<CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius() || r.getMockY()+r.getLength() > CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && r.getMockY()+r.getLength() < CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius()){
				hasNotCollided=false;	
				if (CircleUnits.get(y).isEnemy!= r.isEnemy() && damaging==1){
				CircleUnits.get(y).setHealth(CircleUnits.get(y).getHealth()-r.getDamage());	
				r.setHealth(r.getHealth()-CircleUnits.get(y).getDamage());
				}
				} 	
				}		
	}
		for (int z = 0; z<towers.size(); z++){
			if(r.getMockX()>towers.get(z).getX()-towers.get(z).getRadius() && r.getMockX()<towers.get(z).getX()+towers.get(z).getRadius() || r.getMockX()+r.getLength() > towers.get(z).getX()-towers.get(z).getRadius() && r.getMockX()+r.getLength() < towers.get(z).getX()+towers.get(z).getRadius()){
				if(r.getMockY()>towers.get(z).getY()-towers.get(z).getRadius() && r.getMockY()<towers.get(z).getY()+towers.get(z).getRadius() || r.getMockY()+r.getLength() > towers.get(z).getY()-towers.get(z).getRadius() && r.getMockY()+r.getLength() < towers.get(z).getY()+towers.get(z).getRadius()){
				hasNotCollided=false;	
				if (towers.get(z).isEnemy!= r.isEnemy() && damaging==1){
				towers.get(z).setHealth(towers.get(z).getHealth()-r.getDamage());	
				}
				} 	
				}			
	}	
		if(r.getMockX()>main.getX() && r.getMockX()<main.getX()+main.getLength() || r.getMockX()+r.getLength() > main.getX() && r.getMockX()+r.getLength() < main.getX()+main.getLength()){
			if(r.getMockY()>main.getY() && r.getMockY()<main.getY()+main.getLength() || r.getMockY()+r.getLength() > main.getY() && r.getMockY()+r.getLength() < main.getY()+main.getLength()){	
			hasNotCollided=false;
			if(r.isEnemy && damaging==1){
			main.setHealth(main.getHealth()-r.getDamage());	
			r.setHealth(r.getHealth()-main.getDamage());	
			}
			}}
				return hasNotCollided;	
	}
	
	public boolean collisionCheck(Circle c, int i, int damaging){
		boolean hasNotCollided=true;
		for (int w = 0; w<buildings.size(); w++){
			if(c.getMockX()-c.getRadius()>buildings.get(w).getX() && c.getMockX()-c.getRadius()<buildings.get(w).getX()+buildings.get(w).getLength() || c.getMockX()+c.getRadius() > buildings.get(w).getX() && c.getMockX()+c.getRadius() < buildings.get(w).getX()+buildings.get(w).getLength()){
				if(c.getMockY()-c.getRadius()>buildings.get(w).getY() && c.getMockY()-c.getRadius()<buildings.get(w).getY()+buildings.get(w).getLength() || c.getMockY()+c.getRadius() > buildings.get(w).getY() && c.getMockY()+c.getRadius() < buildings.get(w).getY()+buildings.get(w).getLength()){	
				hasNotCollided=false;
				if (buildings.get(w).isEnemy!= c.isEnemy() && damaging==1){
				buildings.get(w).setHealth(buildings.get(w).getHealth()-c.getDamage());	
				}
				}}	
		}
	for (int x = 0; x<squares.size(); x++){
		if(c.getMockX()-c.getRadius()>squares.get(x).getX() && c.getMockX()-c.getRadius()<squares.get(x).getX()+squares.get(x).getLength() || c.getMockX()+c.getRadius() > squares.get(x).getX() && c.getMockX()+c.getRadius() < squares.get(x).getX()+squares.get(x).getLength()){
			if(c.getMockY()-c.getRadius()>squares.get(x).getY() && c.getMockY()-c.getRadius()<squares.get(x).getY()+squares.get(x).getLength() || c.getMockY()+c.getRadius() > squares.get(x).getY() && c.getMockY()+c.getRadius() < squares.get(x).getY()+squares.get(x).getLength()){	
			hasNotCollided=false;
			if(c.isEnemy != squares.get(x).isEnemy && damaging==1){
				squares.get(x).setHealth(squares.get(x).getHealth()-c.getDamage());	
				c.setHealth(c.getHealth()-squares.get(x).getDamage());	
				}
			}}
		}
	for (int y = 0; y<CircleUnits.size(); y++){
		if(c.getMockX()-c.getRadius()>CircleUnits.get(y).getX()-CircleUnits.get(y).getRadius() && c.getMockX()-c.getRadius()<CircleUnits.get(y).getX()+CircleUnits.get(y).getRadius() || c.getMockX()+c.getRadius() > CircleUnits.get(y).getX()-CircleUnits.get(y).getRadius() && c.getMockX()+c.getRadius() < CircleUnits.get(y).getX()+CircleUnits.get(y).getRadius()){
			if(c.getMockY()-c.getRadius()>CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && c.getMockY()-c.getRadius()<CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius() || c.getMockY()+c.getRadius() > CircleUnits.get(y).getY()-CircleUnits.get(y).getRadius() && c.getMockY()+c.getRadius() < CircleUnits.get(y).getY()+CircleUnits.get(y).getRadius()){	
			hasNotCollided=false;
			if(c.isEnemy != CircleUnits.get(y).isEnemy && damaging==1){
				CircleUnits.get(y).setHealth(CircleUnits.get(y).getHealth()-c.getDamage());	
				c.setHealth(c.getHealth()-CircleUnits.get(y).getDamage());	
				}
			}}		
}
	for (int z = 0; z<towers.size(); z++){
		if(c.getMockX()-c.getRadius()>towers.get(z).getX()-towers.get(z).getRadius() && c.getMockX()-c.getRadius()<towers.get(z).getX()+towers.get(z).getRadius() || c.getMockX()+c.getRadius() > towers.get(z).getX()-towers.get(z).getRadius() && c.getMockX()+c.getRadius() < towers.get(z).getX()+towers.get(z).getRadius()){
			if(c.getMockY()-c.getRadius()>towers.get(z).getY()-towers.get(z).getRadius() && c.getMockY()-c.getRadius()<towers.get(z).getY()+towers.get(z).getRadius() || c.getMockY()+c.getRadius() > towers.get(z).getY()-towers.get(z).getRadius() && c.getMockY()+c.getRadius() < towers.get(z).getY()+towers.get(z).getRadius()){	
			hasNotCollided=false;
			if (towers.get(z).isEnemy!= c.isEnemy() && damaging==1){
				towers.get(z).setHealth(towers.get(z).getHealth()-c.getDamage());	
				}
			}}	
}	
	if(c.getMockX()-c.getRadius()>main.getX() && c.getMockX()-c.getRadius()<main.getX()+main.getLength() || c.getMockX()+c.getRadius() > main.getX() && c.getMockX()+c.getRadius() < main.getX()+main.getLength()){
		if(c.getMockY()-c.getRadius()>main.getY() && c.getMockY()-c.getRadius()<main.getY()+main.getLength() || c.getMockY()+c.getRadius() > main.getY() && c.getMockY()+c.getRadius() < main.getY()+main.getLength()){	
		hasNotCollided=false;
		if(c.isEnemy && damaging==1){
			main.setHealth(main.getHealth()-c.getDamage());	
			c.setHealth(c.getHealth()-main.getDamage());	
			}
		}}
	return hasNotCollided;	
	}
	
	
	
	
	
	
	
	
	public void setTextView(TextView tv){
		text=tv;	
		text.setVisibility(View.VISIBLE);

		}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		if (keyCode == KeyEvent.KEYCODE_ENTER) {
			int error = 2 / 0;
		}
		return true;
	}

	public Bundle makeMenuBundle(){
		Bundle bundle=new Bundle();
		bundle.putInt("x", main.getX());
		bundle.putInt("y", main.getY());
		bundle.putInt("str",main.getSTR());
		bundle.putInt("int",main.getINT());
		bundle.putInt("con",main.getCON());
		bundle.putInt("dex",main.getDEX());
		bundle.putInt("skill", main.getSkill());
		bundle.putInt("XP", main.getXP());
		bundle.putInt("MaxXP", main.getMaxXP());
		bundle.putInt("Health", main.getHealth());
		bundle.putInt("MaxHealth", main.getMaxHealth());
		bundle.putInt("gold", gold);
		bundle.putInt("mode", mode);
		bundle.putInt("troops", troops);
		bundle.putInt("AiGold", AiGold);
		bundle.putInt("AiTroops", AiTroops);
		bundle.putInt("AiMode", AiMode);
		bundle.putInt("interval", interval);
		return bundle;
		
	}
	 public void restore(ArrayList <Building> a, ArrayList <Turret> c, ArrayList <Square> d, ArrayList <Circle> f){
	 buildings=a;
	 towers=c;
	 squares=d;
	 CircleUnits=f;
	 main.updateStats();
	 }
	 public void unpackMenuBundle(Bundle menuBundle){
		 isBuying=menuBundle.getInt("isBuying");
		 gold=menuBundle.getInt("gold");
		 mode=menuBundle.getInt("mode");
		 AiTroops=menuBundle.getInt("AiTroops");
		 AiMode=menuBundle.getInt("AiMode");
		 AiGold=menuBundle.getInt("AiGold");
		 interval=menuBundle.getInt("interval");
		 troops=menuBundle.getInt("troops");
		 main.setXP(menuBundle.getInt("XP"));
		 main.setMaxXP(menuBundle.getInt("MaxXP"));
		 main.setHealth(menuBundle.getInt("Health"));
		 main.setMaxHealth(menuBundle.getInt("MaxHealth"));
		 main.setX(menuBundle.getInt("x"));	
		 main.setY(menuBundle.getInt("y"));	
		 main.setSTR(menuBundle.getInt("str"));	
		 main.setINT(menuBundle.getInt("int"));	
		 main.setCON(menuBundle.getInt("con"));	
		 main.setDEX(menuBundle.getInt("dex"));	
		 main.setSkill(menuBundle.getInt("skill"));
	 }
	 
	 
	 private RefreshHandler FinalHandler = new RefreshHandler();

		class RefreshHandler extends Handler {
			@Override
			public void handleMessage(Message msg) {
				FinalView.this.update();
				FinalView.this.invalidate();
			}

			public void sleep(long delayMillis) {
				this.removeMessages(0);
				sendMessageDelayed(obtainMessage(0), delayMillis);
			}
		};
		
			public ArrayList<Building> getBuilding(){
			return buildings;	
			}
			public ArrayList<Turret> getTowers(){
				return towers;	
				}
			public ArrayList<Square> getSquares(){
				return squares;	
				}
			public ArrayList<Circle> getCircleUnits(){
				return CircleUnits;	
				}
			public MainShape getMain(){
			return main;	
			}
			public FinalView getFinal(){
			return this;	
			}
			public void setMode(int modee){
			mode=modee;	
			}
}
